package com.alks.function.service.impl.produce;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.UserInfoUtil;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.config.ProfileConfig;
import com.alks.function.data.dto.chengKong.produce.SdStyleSizeDto;
import com.alks.function.data.dto.produce.LabelPartNameDto;
import com.alks.function.data.dto.produce.PartListDto;
import com.alks.function.data.dto.produce.labelDto;
import com.alks.function.data.request.produce.LabelQueryRequest;
import com.alks.function.data.request.produce.LabelRequest;
import com.alks.function.data.request.produce.PartNameQtyRequest;
import com.alks.function.mapper.SdCompanyMapper;
import com.alks.function.mapper.chengKong.PcLotHeadMapper;
import com.alks.function.mapper.pcfactoryquery.FT_QR_PcCuttingLsPartMapper;
import com.alks.function.mapper.pcfactorywork.PcOutListPriceMapper;
import com.alks.function.mapper.produce.ComponentLabelMapper;
import com.alks.function.mapper.produce.CutPlanMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.PcCuttingLsLabelMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.PcLotPartProcessMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcLotPartMapper;
import com.alks.function.service.produce.ComponentLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class ComponentLabelServiceImpl implements ComponentLabelService {

    @Autowired
    private ComponentLabelMapper componentLabelMapper;

    @Autowired
    private PcLotHeadMapper pcLotHeadMapper;

    @Autowired
    private CutPlanMapper cutPlanMapper;

    @Autowired
    private PcOutListPriceMapper pcOutListPriceMapper;

    @Autowired
    private PcCuttingLsLabelMapper pcCuttingLsLabelMapper;

    @Autowired
    private FT_QR_PcCuttingLsPartMapper pcCuttingLsPartMapper;

    @Autowired
    private SD_SQ_PcLotPartMapper pcLotPartMapper;

    @Autowired
    private PcLotPartProcessMapper pcLotPartProcessMapper;

    @Autowired
    private SdCompanyMapper sdCompanyMapper;

    /**
     * 指令明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo ComponentLabelQuery(LabelQueryRequest request) {
        PageRecord<labelDto> list = new PageRecord<>();
        String companyId = UserIdThread.get().getCompanyId();
        request.setCompanyId(companyId);
        List<labelDto> componentLabel = componentLabelMapper.getComponentLabel(request);
        PageInfo pageInfo=new PageInfo(componentLabel);
        list.setList(componentLabel);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 查询标签明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo LabelDetails(LabelRequest request) {
        PageRecord<LabelPartNameDto> list = new PageRecord<>();
        List<LabelPartNameDto> labelDetails = componentLabelMapper.getLabelDetails(request);
        Integer id=1;
        for (LabelPartNameDto labelDetail : labelDetails) {
            labelDetail.setId(id++);
        }
        PageInfo pageInfo = new PageInfo(labelDetails);
        list.setList(labelDetails);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    @Resource
    private ProfileConfig profileConfig;

    /**
     * 确定
     * @param lotNo
     * @param list
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo generate(String lotNo, List<PartNameQtyRequest> list) {
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("无效指令!");
        }
        int minBagQty=1;
        int remainConfigQty=50;
        int maxBagQty=500;
        if (profileConfig.isVNMProfile()){
            maxBagQty=1000;
        }
        if (ObjectUtils.isEmpty(list)){
            return ResponseInfo.error("请选择部位!");
        }
        for (PartNameQtyRequest t : list) {
            if (ObjectUtils.isEmpty(t.getBagQty())){
                return ResponseInfo.error("部位袋数不能为空!");
            }
            if (t.getBagQty()>maxBagQty||t.getBagQty()<minBagQty){
                return ResponseInfo.error("袋装数量有效范围是"+minBagQty+"~"+maxBagQty+"!");
            }
        }
        componentLabelMapper.delPcLotCutPartBag(lotNo,pcLotHead.getCompanyId());
        List<PcCuttingLsDetail> crdList = cutPlanMapper.getBatchNumberDetails(lotNo, pcLotHead.getCompanyId());
        if (ObjectUtils.isEmpty(crdList)){
            return ResponseInfo.error("没有找到冲裁批次信息,请核实!");
        }
        List<PcCuttingLsLabel> labelList = new ArrayList<>();
        List<PartListDto> partList = componentLabelMapper.getPartList(lotNo, pcLotHead.getCompanyId());
        Map<String, PartListDto> map = new HashMap();
        Iterator var8 = partList.iterator();
        while(var8.hasNext()) {
            PartListDto lotPart = (PartListDto)var8.next();
            map.put(lotPart.getPartName(), lotPart);
        }

        LocalDateTime now = LocalDateTime.now();
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();

        Iterator it;
        PcLotCutPartBag pcLotCutPartBag;
        label97:
        for(it = list.iterator(); it.hasNext();this.componentLabelMapper.addPcLotCutPartBag(pcLotCutPartBag)) {
            PartNameQtyRequest lotPart =(PartNameQtyRequest) it.next();
            PartListDto templotPart = (PartListDto)map.get(lotPart.getPartName());
            if (ObjectUtils.isEmpty(templotPart)) {
                throw new ServiceErrorException(lotPart.getPartName()+":部位未找到冲裁批次信息,请核实!");
            }
            pcLotCutPartBag=new PcLotCutPartBag();
            String uuid = UUID.randomUUID().toString();
            pcLotCutPartBag.setUuid(uuid);
            pcLotCutPartBag.setLotNo(pcLotHead.getLotNo());
            pcLotCutPartBag.setCompanyId(pcLotHead.getCompanyId());
            pcLotCutPartBag.setStyleNo(pcLotHead.getStyle());
            pcLotCutPartBag.setStyleColor(pcLotHead.getColor());
            pcLotCutPartBag.setCreateTime(now);
            pcLotCutPartBag.setCreateUserName(userInfo.getUserName());
            pcLotCutPartBag.setPartName(templotPart.getPartName());
            pcLotCutPartBag.setSeqNo(templotPart.getSeqNo());

            Iterator var16;
            PcCuttingLsDetail crd;
            if (!ObjectUtils.isEmpty(lotPart.getBagQty()) && lotPart.getBagQty() != 0) {
                pcLotCutPartBag.setBagQty(lotPart.getBagQty());
                var16 = crdList.iterator();
                while (true){
                    do {
                        if (!var16.hasNext()){
                            continue label97;
                        }
                        crd = (PcCuttingLsDetail) var16.next();
                    }while (crd.getQty()<=0);

                    int count = (int)Math.ceil(Double.valueOf((double)crd.getQty()) / (double)lotPart.getBagQty());
                    int mantissa = crd.getQty() % lotPart.getBagQty();
                    if (mantissa > 0 && mantissa <= remainConfigQty && count > 1) {
                        --count;
                    }

                    int remainQty = crd.getQty();
                    PcCuttingLsLabel pcCuttingLsLabel;
                    for(int curIndex = 0; remainQty > 0; labelList.add(pcCuttingLsLabel)) {
                        ++curIndex;
                        int realQty = remainQty;
                        if (remainQty > lotPart.getBagQty()) {
                            realQty = lotPart.getBagQty();
                        }

                        String sizeNo = crd.getSizeNo();
                        String sizeNum = sizeNo.replaceAll("\\D", "");
                        if (sizeNum.length() == 0) {
                            sizeNum = "0";
                        }
                        sizeNo = String.format("%03d", Integer.valueOf(sizeNum));
                        String label = String.format("%s%02d%s%02d%d", pcLotHead.getLotNo(), templotPart.getSeqNo(), sizeNo, crd.getLsNo(), curIndex);
                        pcCuttingLsLabel = new PcCuttingLsLabel();
                        pcCuttingLsLabel.setCompanyId(pcLotHead.getCompanyId());
                        pcCuttingLsLabel.setLabelNo(label);
                        pcCuttingLsLabel.setLotNo(pcLotHead.getLotNo());
                        pcCuttingLsLabel.setPartName(templotPart.getPartName());
                        pcCuttingLsLabel.setLsNo(crd.getLsNo());
                        pcCuttingLsLabel.setSizeNo(crd.getSizeNo());
                        pcCuttingLsLabel.setBatchNo(curIndex);
                        pcCuttingLsLabel.setBatchCode(String.format("%d-%d", count, curIndex));
                        pcCuttingLsLabel.setQty(realQty);
                        pcCuttingLsLabel.setStyle(pcLotHead.getStyle());
                        pcCuttingLsLabel.setStyleColor(pcLotHead.getColor());
                        remainQty -= realQty;
                        if (remainQty <= remainConfigQty) {
                            pcCuttingLsLabel.setQty(realQty + remainQty);
                            remainQty = 0;
                        }
                    }
                }
            }else {
                pcLotCutPartBag.setBagQty(templotPart.getQty());
                var16 = crdList.iterator();

                while(var16.hasNext()) {
                    crd = (PcCuttingLsDetail)var16.next();
                    if (crd.getQty() > 0) {
                        String sizeNo = crd.getSizeNo();
                        String sizeNum = sizeNo.replaceAll("\\D", "");
                        if (sizeNum.length() == 0) {
                            sizeNum = "0";
                        }
                        sizeNo = String.format("%03d", Integer.valueOf(sizeNum));
                        String label = String.format("%s%02d%s%02d1", pcLotHead.getLotNo(), templotPart.getSeqNo(), sizeNo, crd.getLsNo());
                        PcCuttingLsLabel pcCuttingLsLabel = new PcCuttingLsLabel();
                        pcCuttingLsLabel.setCompanyId(pcLotHead.getCompanyId());
                        pcCuttingLsLabel.setLabelNo(label);
                        pcCuttingLsLabel.setLotNo(pcLotHead.getLotNo());
                        pcCuttingLsLabel.setPartName(templotPart.getPartName());
                        pcCuttingLsLabel.setLsNo(crd.getLsNo());
                        pcCuttingLsLabel.setSizeNo(crd.getSizeNo());
                        pcCuttingLsLabel.setBatchNo(1);
                        pcCuttingLsLabel.setBatchCode("1-1");
                        pcCuttingLsLabel.setQty(crd.getQty());
                        pcCuttingLsLabel.setStyle(pcLotHead.getStyle());
                        pcCuttingLsLabel.setStyleColor(pcLotHead.getColor());
                        labelList.add(pcCuttingLsLabel);
                    }
                }
            }
        }
        it = labelList.iterator();
        ArrayList waitInsert = new ArrayList();

        while(it.hasNext()) {
            waitInsert.add(it.next());
            if (waitInsert.size() == 100) {
                pcCuttingLsLabelMapper.addPcCuttingLsLabelList(waitInsert);
                waitInsert.clear();
            }
        }
        if (waitInsert.size() > 0) {
            pcCuttingLsLabelMapper.addPcCuttingLsLabelList(waitInsert);
        }
        //从pc_lot_part_process和pc_cutting_ls_detail获取数据
        List<PcCuttingLsPart> detail = pcCuttingLsPartMapper.getProcessTodetail(pcLotHead.getLotNo(), pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(detail)){
            for (int i = 0; i < detail.size(); i += 100) {
                int toIndex = Math.min(i + 100, detail.size());
                List<PcCuttingLsPart> batchList = detail.subList(i, toIndex);
                pcCuttingLsPartMapper.addPcCuttingLsPartList(batchList);
            }
        }
        List<PcCutPartStatus> partProcessList = cutPlanMapper.getPartProcessList(pcLotHead.getLotNo(), pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(partProcessList)){
            for (int i = 0; i < partProcessList.size(); i += 100) {
                int toIndex = Math.min(i + 100, partProcessList.size());
                List<PcCutPartStatus> batchList = partProcessList.subList(i, toIndex);
                cutPlanMapper.addPcCutPartStatusList(batchList);
            }
        }
        List<PcStitchPartStatus> partProcessList2 = cutPlanMapper.getPartProcessList2(pcLotHead.getLotNo(), pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(partProcessList2)){
            for (int i = 0; i < partProcessList2.size(); i += 100) {
                int toIndex = Math.min(i + 100, partProcessList2.size());
                List<PcStitchPartStatus> batchList = partProcessList2.subList(i, toIndex);
                cutPlanMapper.addPcStitchPartStatus(batchList);
            }
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 生成轮次标签
     * @param lotNo
     * @param override
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo checkLsInfo(String lotNo, boolean override) {
        String companyId = UserIdThread.get().getCompanyId();
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo).eq(PcLotHead::getCompanyId,companyId);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("指令资料未输入,请联系业务部!");
        }
        List<PcCuttingDispatchDetail> cddList = componentLabelMapper.selectByLotNo(lotNo, pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(cddList)){
            return ResponseInfo.error("已派工的指令不能重新生成标签!");
        }
        Integer countByLotNo = cutPlanMapper.countByLotNo(pcLotHead.getCompanyId(), lotNo);
        if (countByLotNo != null&&countByLotNo>0){
            return ResponseInfo.error("已生产的轮数不能删除");
        }
        String sdStyleProcessHead = pcOutListPriceMapper.getSdStyleProcessHead(pcLotHead.getStyle(), pcLotHead.getColor(), companyId);
        if (!"Y".equals(sdStyleProcessHead)){
            return ResponseInfo.error("指令的型体/配色工序资料未检核!");
        }
        List<PcLotPart> styleProcessNew = pcLotPartMapper.getStyleProcessNew(pcLotHead.getStyle(), pcLotHead.getColor(), pcLotHead.getCompanyId());
        if (ObjectUtils.isEmpty(styleProcessNew)){
            return ResponseInfo.error("指令的型体/配色工序未定义，请先取数定义!");
        }
        List<PcCuttingLsLabel> pcCuttingLsLabels = cutPlanMapper.selectByLotNos(lotNo, pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(pcCuttingLsLabels)&&!override){
            return ResponseInfo.warn("已存在,是否覆盖!");
        }
        LambdaQueryWrapper<PcCuttingLsLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PcCuttingLsLabel::getLotNo,lotNo).eq(PcCuttingLsLabel::getCompanyId,pcLotHead.getCompanyId());
        pcCuttingLsLabelMapper.delete(lambdaQueryWrapper);
        LambdaQueryWrapper<PcCuttingLsPart> lsPartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        lsPartLambdaQueryWrapper.eq(PcCuttingLsPart::getLotNo,lotNo).eq(PcCuttingLsPart::getCompanyId,pcLotHead.getCompanyId());
        pcCuttingLsPartMapper.delete(lsPartLambdaQueryWrapper);
        cutPlanMapper.delPcCutPartStatus(pcLotHead.getCompanyId(),lotNo);
        componentLabelMapper.delPcStitchPartStatus(pcLotHead.getCompanyId(),lotNo);
        LambdaQueryWrapper<PcLotPart> lotPartWrapper = new LambdaQueryWrapper<>();
        lotPartWrapper.eq(PcLotPart::getLotNo,lotNo).eq(PcLotPart::getCompanyId,pcLotHead.getCompanyId());
        pcLotPartMapper.delete(lotPartWrapper);
        LambdaQueryWrapper<PcLotPartProcess> pcLotPartProcessWrapper = new LambdaQueryWrapper<>();
        pcLotPartProcessWrapper.eq(PcLotPartProcess::getLotNo,lotNo).eq(PcLotPartProcess::getCompanyId,pcLotHead.getCompanyId());
        pcLotPartProcessMapper.delete(pcLotPartProcessWrapper);

        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        //查询工艺信息
        List<PcLotPart> styleProcess = pcLotPartMapper.getStyleProcessNew(pcLotHead.getStyle(), pcLotHead.getColor(), pcLotHead.getCompanyId());
        for (PcLotPart pcLotPart : styleProcess) {
            pcLotPart.setLotNo(lotNo);
            pcLotPart.setSysDate(LocalDateTime.now());
            pcLotPart.setUserId(userInfo.getLoginName());
            pcLotPartMapper.insert(pcLotPart);
        }

        //冲裁工序code
        List<PcLotPartProcess> styleProcessNewList = pcLotPartProcessMapper.getStyleProcessNewList(pcLotHead.getStyle(), pcLotHead.getColor(), pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(styleProcessNewList)){
            for (PcLotPartProcess pcLotPartProcess : styleProcessNewList) {
                pcLotPartProcess.setLotNo(lotNo);
                pcLotPartProcess.setSeqNo("01");
                pcLotPartProcess.setProcessCode("01");
                pcLotPartProcessMapper.insert(pcLotPartProcess);
            }
        }

        Set<PcLotPartProcess> styleProcessNewList2 = pcLotPartProcessMapper.getStyleProcessNewList2(pcLotHead.getStyle(), pcLotHead.getColor(), pcLotHead.getCompanyId(), "02");
        if (!ObjectUtils.isEmpty(styleProcessNewList2)){
            for (PcLotPartProcess pcLotPartProcess : styleProcessNewList2) {
                pcLotPartProcess.setLotNo(lotNo);
                pcLotPartProcess.setSeqNo("02");
                pcLotPartProcess.setProcessCode("02");
                pcLotPartProcessMapper.insert(pcLotPartProcess);
            }
        }

        Set<PcLotPartProcess> styleProcessNewList3 = pcLotPartProcessMapper.getStyleProcessNewList3(pcLotHead.getStyle(), pcLotHead.getColor(), pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(styleProcessNewList3)){
            for (PcLotPartProcess pcLotPartProcess : styleProcessNewList3) {
                pcLotPartProcess.setLotNo(lotNo);
                pcLotPartProcess.setSeqNo("03");
                pcLotPartProcess.setProcessCode("03");
                pcLotPartProcessMapper.insert(pcLotPartProcess);
            }
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 冲裁部位袋装定义查询
     * @param lotNo
     * @return
     */
    @Override
    public ResponseInfo partList(String lotNo) {
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("请输入正确的指令号!");
        }
        List<PartListDto> partList = componentLabelMapper.getPartList(lotNo, pcLotHead.getCompanyId());
        return ResponseInfo.ok(partList);
    }

    /**
     * 删除轮次标签
     * @param lotNo
     * @return
     */
    @Override
    public ResponseInfo deleteLabel(String lotNo) {
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("无效指令!");
        }
        List<PcCuttingDispatchDetail> cddList = componentLabelMapper.selectByLotNo(lotNo, pcLotHead.getCompanyId());
        if (!ObjectUtils.isEmpty(cddList)){
            return ResponseInfo.error("已派工的指令不能重新生成标签!");
        }
        Integer countByLotNo = cutPlanMapper.countByLotNo(pcLotHead.getCompanyId(), lotNo);
        if (countByLotNo != null&&countByLotNo>0){
            return ResponseInfo.error("已生产的轮数不能删除");
        }
        LambdaQueryWrapper<PcCuttingLsLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PcCuttingLsLabel::getLotNo,lotNo).eq(PcCuttingLsLabel::getCompanyId,pcLotHead.getCompanyId());
        pcCuttingLsLabelMapper.delete(lambdaQueryWrapper);
        LambdaQueryWrapper<PcCuttingLsPart> lsPartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        lsPartLambdaQueryWrapper.eq(PcCuttingLsPart::getLotNo,lotNo).eq(PcCuttingLsPart::getCompanyId,pcLotHead.getCompanyId());
        pcCuttingLsPartMapper.delete(lsPartLambdaQueryWrapper);
        cutPlanMapper.delPcCutPartStatus(pcLotHead.getCompanyId(),lotNo);
        componentLabelMapper.delPcStitchPartStatus(pcLotHead.getCompanyId(),lotNo);
        LambdaQueryWrapper<PcLotPart> lotPartWrapper = new LambdaQueryWrapper<>();
        lotPartWrapper.eq(PcLotPart::getLotNo,lotNo).eq(PcLotPart::getCompanyId,pcLotHead.getCompanyId());
        pcLotPartMapper.delete(lotPartWrapper);
        LambdaQueryWrapper<PcLotPartProcess> pcLotPartProcessWrapper = new LambdaQueryWrapper<>();
        pcLotPartProcessWrapper.eq(PcLotPartProcess::getLotNo,lotNo).eq(PcLotPartProcess::getCompanyId,pcLotHead.getCompanyId());
        pcLotPartProcessMapper.delete(pcLotPartProcessWrapper);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 查询指令最大轮次
     * @param lotNo
     * @return
     */
    @Override
    public ResponseInfo loadMaxlsNo(String lotNo) {
        String companyId = UserIdThread.get().getCompanyId();
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo).eq(PcLotHead::getCompanyId,companyId);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("无效指令!");
        }
        Integer maxlsNo=componentLabelMapper.loadMaxlsNo(companyId,lotNo);
        return ResponseInfo.ok(maxlsNo);
    }

    /**
     * 部件标签打印
     * @param partNames
     * @param lotNo
     * @param lsFrom
     * @param lsEnd
     * @param sizeNos
     * @return
     */
    @Override
    public ResponseInfo getPrintLabelList(List<String> partNames, String lotNo, Integer lsFrom, Integer lsEnd, List<String> sizeNos) {
        String companyId = UserIdThread.get().getCompanyId();
        LambdaQueryWrapper<PcLotHead> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcLotHead::getLotNo,lotNo).eq(PcLotHead::getCompanyId,companyId);
        PcLotHead pcLotHead = pcLotHeadMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(pcLotHead)){
            return ResponseInfo.error("指令不存在!");
        }
        if (ObjectUtils.isEmpty(partNames)){
            return ResponseInfo.error("未选择部位!");
        }
        LambdaQueryWrapper<SdCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SdCompany::getCompanyId,companyId);
        SdCompany sdCompany = sdCompanyMapper.selectOne(queryWrapper);
        List<LabelPartNameDto> printLabelList = componentLabelMapper.getPrintLabelList(partNames, lotNo, lsFrom, lsEnd, sizeNos, companyId);
        if (printLabelList.isEmpty()) {
            return ResponseInfo.error("打印标签列表为空!");
        }
        List<SdStyleSizeDto> details = pcLotHeadMapper.getStyleShoeLastDetails(printLabelList.get(0).getStyle(), companyId);
        Map<String, SdStyleSizeDto> styleSizeMap = new HashMap<>();
        for (SdStyleSizeDto detail : details) {
            styleSizeMap.put(detail.getSizeNo(), detail);
        }
        for (LabelPartNameDto t : printLabelList) {
            t.setCompanyId(sdCompany.getAbbrName());
            if (!ObjectUtils.isEmpty(styleSizeMap)){
                t.setCutSize(styleSizeMap.get(t.getSizeNo()).getCutSize());
            }
        }
        return ResponseInfo.ok(printLabelList);
    }
}
